﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ECube.Web.CMS.Models;
namespace  ECube.Web.CMS.Extensions
{
    /// <summary>
    /// SiteMapNode extensions
    /// </summary>
    public static class SiteMapNodeExtensions
    {
        /// <summary>
        /// Gets the level of the current SiteMapNode
        /// </summary>
        /// <param name="current">The current SiteMapNode</param>
        /// <returns>The level of the current SiteMapNode</returns>
        public static int GetNodeLevel(this SiteMapNode current)
        {
            int level = 0;
            SiteMapNode node = current;

            while (node.ParentNode != null)
            {
                level++;
                node = node.ParentNode;
            }

            return level;
        }
        
        public static void FromInput(this SiteMapNode node, MvcSiteMapNodeInput input)
        {
            node.Title = input.Title;
            node.Url = input.Url;
            node.Description = input.Description;
            MvcSiteMapNode mvcNode = node as MvcSiteMapNode;
            if (mvcNode != null)
            {
                mvcNode.Area = input.Area;
                mvcNode.Controller = input.Controller;
                mvcNode.Action = input.Action;
                mvcNode.DynamicParameters.Clear();
                if (input.DynamicParameters != null && input.DynamicParameters.Count > 0)
                {
                    mvcNode.DynamicParameters.AddRange(input.DynamicParameters.ToList());
                }
                mvcNode.Group = input.Group;
                mvcNode.IsDynamic = input.IsDynamic;
                mvcNode.PageId = input.PageId;
                mvcNode.ReadOnly = input.ReadOnly;
                mvcNode.ResourceKey = input.ResourceKey;

            }
            if (string.IsNullOrEmpty(node.Url))
            {
                node.Url = "~/" + node.Key;
            }
                  
        }
        public static MvcSiteMapNodeInput ToInput(this SiteMapNode node)
        {
            MvcSiteMapNodeInput input = new MvcSiteMapNodeInput();
            input.Key = node.Key;
            input.Title = node.Title;
            input.Url = node.Url;
            if(node.ParentNode != null)
            {
                input.ParentKey = node.ParentNode.Key;
            }
            input.Description = node.Description;
            MvcSiteMapNode mvcNode = node as MvcSiteMapNode;
            if (mvcNode != null)
            {
                input.Area = mvcNode.Area;
                input.Controller = mvcNode.Controller;
                input.Action = mvcNode.Action;
                input.DynamicParameters = mvcNode.DynamicParameters.ToList();
                input.Group = mvcNode.Group;
                input.IsDynamic = mvcNode.IsDynamic;
                input.PageId = mvcNode.PageId;
                input.ReadOnly = mvcNode.ReadOnly;
                input.ResourceKey = mvcNode.ResourceKey; 
            }
            input.Status = MvcSiteMapNodeStatus.Original;
            return input;
        }
    }
}
